package map.android.baidu.carpool.reservation;

import java.util.ArrayList;

import com.baidu.components.api.extra.APIProxy;

import android.app.Activity;
import android.app.Dialog;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import map.android.baidu.carpool.R;
import map.android.baidu.carpool.model.AirportTransCity.Terminal;
import map.android.baidu.carpool.widget.wheel.OnWheelChangedListener;
import map.android.baidu.carpool.widget.wheel.WheelView;

public class CarTerminalSelectDialog extends Dialog {
	private Activity mActivity;
	
	private ArrayList<Terminal> mAllTerminalList;
	
	public static final boolean VISIBLE = true;
	public static final boolean GONE = false;
	
	private WheelView terminalWheelView = null;
	private TerminalWheelAdapter terminalAdapter = null;

	private OnSelectTerminalListener terminalSelectListener ;
	private static final double SCALE = 1.0;

	public interface OnSelectTerminalListener{
		void terminalSelected(Terminal terminal);
	}
	

	public CarTerminalSelectDialog(Activity activity,  int style,OnSelectTerminalListener terminalSelectListener,ArrayList<Terminal> mAllTerminalList) {
		super(activity, style);
		this.mActivity = activity;
		this.mAllTerminalList = mAllTerminalList;
		this.terminalSelectListener = terminalSelectListener;
		init();
	}

	@SuppressWarnings("deprecation")
	private void init() {
		LayoutInflater inflater = APIProxy.res().getInflater();
		View view = inflater.inflate(R.layout.car_dialog_terminal_select_layout, null);

		setContentView(view);

		WindowManager windowManager = mActivity.getWindowManager();
		Display display = windowManager.getDefaultDisplay();

		Window window = getWindow();
		WindowManager.LayoutParams attributes = window.getAttributes();
		attributes.gravity = Gravity.BOTTOM;
		attributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
		attributes.width = (int) (display.getWidth() * SCALE);
		window.setAttributes(attributes);

		initViewUseNew(view);
	}

	private void initViewUseNew(View view) {
		try {
			LinearLayout wheelContainerView = (LinearLayout) view.findViewById(R.id.car_wheel_container);
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
			lp.weight = 0.33f;

			terminalAdapter = new  TerminalWheelAdapter(mActivity);
			terminalAdapter.setData(mAllTerminalList);
			terminalWheelView = new WheelView(mActivity);
			wheelContainerView.addView(terminalWheelView, lp);
			terminalWheelView.setCurrentItem(0);
			terminalWheelView.setViewAdapter(terminalAdapter);
			terminalAdapter.setItemResource(R.layout.car_wheel_text_item);
			terminalAdapter.setItemTextResource(R.id.car_reservation_wheelview_text);
			terminalWheelView.addChangingListener(new OnWheelChangedListener() {
				@Override
				public void onChanged(WheelView wheel, int oldValue, int newValue) {
					
				}
			});
			TextView okTextView = (TextView) view.findViewById(R.id.car_reservation_wheel_ok_textview);
			okTextView.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					if (terminalSelectListener != null) {
						Terminal terminal = terminalAdapter.getItem(terminalWheelView.getCurrentItem());
						terminalSelectListener.terminalSelected(terminal);
					}
					dismiss();
				}
			});
			TextView cancelTextView = (TextView) view.findViewById(R.id.car_reservation_wheel_cancel_textview);
			cancelTextView.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					dismiss();
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
